package org.jeecg.modules.teacher.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.system.vo.SysTenantVo;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.classroom.vo.FacClassroomVo;
import org.jeecg.modules.relation.relclssubtearoom.entity.RelClassSubjectTeacher;
import org.jeecg.modules.relation.relclssubtearoom.mapper.RelClassSubjectTeacherMapper;
import org.jeecg.modules.student.entity.StuStudent;
import org.jeecg.modules.teacher.entity.TeaTeacher;
import org.jeecg.modules.teacher.mapper.TeaTeacherMapper;
import org.jeecg.modules.teacher.service.ITeaTeacherService;
import org.jeecg.modules.teacher.vo.TeaTeacherVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import javax.swing.text.html.parser.Entity;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: tea_teacher
 * @author: baochangrong
 * @Date:   2022-02-15
 * @Version: V1.0
 */
@Service
public class TeaTeacherServiceImpl extends ServiceImpl<TeaTeacherMapper, TeaTeacher> implements ITeaTeacherService {

    @Autowired
    TeaTeacherMapper teaTeacherMapper;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    RelClassSubjectTeacherMapper relClassSubjectTeacherMapper;

    /** 全部列表查询 **/
    @Override
    public List<TeaTeacher> queryTeacherLists(String tenantId){
        List<TeaTeacher> listdatas =  teaTeacherMapper.queryTeacherLists(tenantId);
        List<TeaTeacher> newList = new ArrayList<TeaTeacher>();
        System.out.println(listdatas);
//        for (TeaTeacher list : listdatas) {
//            list.setTeacherName(list.getTeacherName() + "(" + list.getTeacherSubjectName() + ")");
//            newList.add(list);
//        }
        return newList;
    }
    @Override
    public IPage<TeaTeacherVo> queryTeacherVoLists(Page<TeaTeacherVo> page,TeaTeacherVo teaTeacherVo) {
        //获取该租户下所有老师
        List<TeaTeacherVo> teaTeacherVoList = teaTeacherMapper.queryTeacherVoLists(page, teaTeacherVo);

        return page.setRecords(teaTeacherVoList);
    }


    @Override
    public TeaTeacher getTeacherByUserId(String userId){
        return teaTeacherMapper.getTeacherByUserId(userId);
    }
    @Override
    public boolean createUserByTeacherIds(String tenantId,String password,String ids, LoginUser loginUser,boolean force){
        if (oConvertUtils.isEmpty(ids)) {
            return false;
        }
//        Collection<TeaTeacher> idList2 = idList;
//        List teacherList = listByIds(idList);
        List idsli = Arrays.asList(ids.split(","));

//        List<TeaTeacher> teacherList = getTeacherListByIds(ids);
        List<TeaTeacher>  teacherList = listByIds(idsli);
        SysTenantVo sysTenantVo = sysBaseAPI.getSysTenant(tenantId);

        //复制当前登录用户
        String oldUserJSON = JSONObject.toJSONString(loginUser);
        LoginUser newUser = JSONObject.parseObject(oldUserJSON,LoginUser.class);
        for(TeaTeacher teaTeacher : teacherList) {
            //判断是否为空，为空则执行，避免重复执行
            if( oConvertUtils.isEmpty(teaTeacher.getUserId()) ) {
                //是否强制执行创建用户（已有用户，重新创建新用户）
                if(force){
                    teaTeacher.setUserId(null);
                }
                if(oConvertUtils.isNotEmpty(teaTeacher.getTeacherName()) ){
                    newUser.setUsername(sysTenantVo.getPreCode() + teaTeacher.getTeacherName()); //优先使用租户后缀+教师姓名
                }
                else if(oConvertUtils.isNotEmpty(teaTeacher.getTeacherFacultyCode())){
                    newUser.setUsername(sysTenantVo.getPreCode() + teaTeacher.getTeacherFacultyCode()); //其次使用教职工号
                }
                else{
                    newUser.setUsername(teaTeacher.getTeacherIdNumber()); //其次使用身份证号
                }
                newUser.setPassword(password);
                newUser.setUserIdentity(3);//身份(字典码user_identity)：3老师 4教师  5家长 1.用户 2部门负责人 6虚拟班级
                newUser.setId(null);

                newUser.setRealname(teaTeacher.getTeacherName());
                newUser.setBirthday(teaTeacher.getTeacherBirth());
                newUser.setOrgCode(teaTeacher.getSysOrgCode());
                newUser.setSex(teaTeacher.getTeacherGender());
                newUser.setPhone(teaTeacher.getTeacherTel()); //电话
                //工号，新增
                newUser.setWorkNo(sysTenantVo.getPreCode() + teaTeacher.getTeacherFacultyCode());//工号：租号+教职工号
                newUser.setStatus(1); //正常，未冻结
                newUser.setDelFlag(0); //正常未删除
                newUser.setTelephone(teaTeacher.getTeacherTel()); //座机号
                newUser.setEmail(teaTeacher.getTeacherEmail());
                newUser.setDepartIds(sysBaseAPI.getDepartIdsByOrgCode(teaTeacher.getSysOrgCode())); //部门编码设置部门id

//                String newuserId = sysBaseAPI.addUser(tenantId,newUser);
                sysBaseAPI.addUser(tenantId,newUser);

                //更新教师的用户id
                teaTeacher.setUserId(newUser.getId());
                updateById(teaTeacher);
            }
        }

        //全部执行成功
        return true;
    }


    /**
     * 查询班级的已分配科目教师
     */
    @Override
    public List<TeaTeacher> queryTeacherLists(String tenantId,String classId){
        //查询班级已分配科目教师表
        RelClassSubjectTeacher classSubjectTeacher = new RelClassSubjectTeacher();
        classSubjectTeacher.setClassId(classId);
        classSubjectTeacher.setTenantId(tenantId);
        List<RelClassSubjectTeacher> ObjList = relClassSubjectTeacherMapper.selectList(
                new QueryWrapper<RelClassSubjectTeacher>(classSubjectTeacher));
        //通过查询到的关系数据，查询教师对象数据
        //定义返回集合
        List<TeaTeacher> teaTeachers = new ArrayList<>();
        for (RelClassSubjectTeacher item : ObjList) {
            TeaTeacher teaTeacher = teaTeacherMapper.selectById(item.getTeacherId());
            teaTeachers.add(teaTeacher);
        }
        return teaTeachers;
    }




}
